package C016;

import java.util.Deque;
import java.util.LinkedList;

public class LinkedListImple {
    public static void main(String[] args) {
        MyCircularDeque circularDeque = new MyCircularDeque(3); // 设置容量大小为3
        System.out.println(circularDeque.insertLast(1));                    // 返回 true
        System.out.println(circularDeque.insertLast(2));                    // 返回 true
        System.out.println(circularDeque.insertFront(3));                    // 返回 true
        System.out.println(circularDeque.insertFront(4));                    // 已经满了，返回 false
        System.out.println(circularDeque.getRear());                // 返回 2
        System.out.println(circularDeque.isFull());                        // 返回 true
        System.out.println(circularDeque.deleteLast());                    // 返回 true
        System.out.println(circularDeque.insertFront(4));                    // 返回 true
        System.out.println(circularDeque.getFront());                // 返回 4
    }
}
class MyCircularDeque02 {
    // deque 是双端队列的意思
    Deque<Integer> deque = new LinkedList<>();
    int limit;
    public MyCircularDeque02(int k) {
        limit = k;
    }
    
    public boolean insertFront(int value) {
        if(deque.size() == limit) {
            return false;
        }
        deque.offerFirst(value);
        return true;
    }
    
    public boolean insertLast(int value) {
        if(deque.size() == limit) {
            return false;
        }
        deque.offerLast(value);
        return true;
    }
    
    public boolean deleteFront() {
        if(deque.isEmpty()) {
            return false;
        }
        deque.pollFirst();
        return true;
    }
    
    public boolean deleteLast() {
        if(deque.isEmpty()) {
            return false;
        }
        deque.pollLast();
        return true;
    }
    
    public int getFront() {
        if(deque.isEmpty()) {
            return -1;
        }
        return deque.peekFirst();
    }
    
    public int getRear() {
        if(deque.isEmpty()) {
            return -1;
        }
        return deque.peekLast();
    }
    
    public boolean isEmpty() {
        return deque.isEmpty();
    }
    
    public boolean isFull() {
        return deque.size() == limit;
    }
}

/**
 * Your MyCircularDeque object will be instantiated and called as such:
 * MyCircularDeque obj = new MyCircularDeque(k);
 * boolean param_1 = obj.insertFront(value);
 * boolean param_2 = obj.insertLast(value);
 * boolean param_3 = obj.deleteFront();
 * boolean param_4 = obj.deleteLast();
 * int param_5 = obj.getFront();
 * int param_6 = obj.getRear();
 * boolean param_7 = obj.isEmpty();
 * boolean param_8 = obj.isFull();
 */